package com.techblogon.serviceexample.network;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener;
/*
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonParser;
*/
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ContentProviderOperation;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Typeface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager.LayoutParams;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

public class Main {//extends Activity {
	/** Called when the activity is first created. */
	public int iLanguage = 0;
	TextView lbl;
	Typeface arabicFont = null;
	int TIMEOUT_MILLISEC = 10000; // = 10 seconds
	
	JSONObject json;
	HttpPost request;
	HttpClient client;
	
	public Main(){
		//json = new JSONObject();
	}
	public void f(){
		try{
			for (int i = 0; i < 40; i++) {
				
			
		Thread.sleep(2000);
		 Calendar cal = Calendar.getInstance();

	      // print current time
	    //  System.out.println(" Current time is : " + cal.getTime());
		clickbuttonRecieve(i+"test"+ cal.getTime());}
		}
		catch(Exception e) {}
	}
	/*@Override
	public void onCreate(Bundle savedInstanceState) {
		try {

			super.onCreate(savedInstanceState);
			setContentView(R.layout.main);
			getWindow().setLayout(LayoutParams.FILL_PARENT,
					LayoutParams.FILL_PARENT);
			// ////
			arabicFont = Typeface.createFromAsset(getAssets(),
					"fonts/DroidSansArabic.ttf");

			lbl = (TextView) findViewById(R.id.lbl);
			//

		} catch (Throwable t) {
			Toast.makeText(this, "Request failed: " + t.toString(),
					Toast.LENGTH_LONG).show();
		}
	}*/
	void ustawienia(String adresUrl){
		try {
		HttpParams httpParams = new BasicHttpParams();
		HttpConnectionParams.setConnectionTimeout(httpParams,
				TIMEOUT_MILLISEC);
		HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
		client = new DefaultHttpClient(httpParams);
		request = new HttpPost(adresUrl);
		request.setEntity(new ByteArrayEntity(json.toString().getBytes(
				"UTF8")));
		} catch (Throwable t) {
			//Toast.makeText(this, "Request failed: " + t.toString(),
			//		Toast.LENGTH_LONG).show();
		}
	}
	public void dodajuzytkownika(String nazwa,String pass){
		
		try {
			JSONObject json = new JSONObject();
			json.put("UserName", "a");
			json.put("Password", "a1");
			HttpParams httpParams = new BasicHttpParams();
			HttpConnectionParams.setConnectionTimeout(httpParams,
					TIMEOUT_MILLISEC);
			HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
			HttpClient client = new DefaultHttpClient(httpParams);
			String url = "http://opiekuj1.javaprovider.net/php/test.php";

			HttpPost request = new HttpPost(url);
			request.setEntity(new ByteArrayEntity(json.toString().getBytes(
					"UTF8")));
			request.setHeader("json", json.toString());
			HttpResponse response = client.execute(request);
			HttpEntity entity = response.getEntity();
			if (entity != null) {
				InputStream instream = entity.getContent();

				String result = RestClient.convertStreamToString(instream);
				Log.i("Read from server", result);
			//	Toast.makeText(this,  result,
			//			Toast.LENGTH_LONG).show();
			}
			
		} 
		catch (Exception e ){
			
			//Toast.makeText(this, "Request failed: " + t.toString(),
			//		Toast.LENGTH_LONG).show();
		}
		
		
	}
	public void dodaj(String ... p){
		try {
			JSONObject json = new JSONObject();
			json.put("UserName", p[0]);
			json.put("Password", p[1]);
			HttpParams httpParams = new BasicHttpParams();
			HttpConnectionParams.setConnectionTimeout(httpParams,
					TIMEOUT_MILLISEC);
			HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
			HttpClient client = new DefaultHttpClient(httpParams);
			String url = "opiekuj1.javaprovider.net/php/test.php";

			HttpPost request = new HttpPost(url);
			request.setEntity(new ByteArrayEntity(json.toString().getBytes(
					"UTF8")));
			request.setHeader("json", json.toString());
			HttpResponse response = client.execute(request);
			HttpEntity entity = response.getEntity();
			if (entity != null) {
				InputStream instream = entity.getContent();

				String result = RestClient.convertStreamToString(instream);
				Log.i("Read from server", result);
			//	Toast.makeText(this,  result,
			//			Toast.LENGTH_LONG).show();
			}
		} catch (Throwable t) {
			//Toast.makeText(this, "Request failed: " + t.toString(),
				//	Toast.LENGTH_LONG).show();
		}
	}
	public void clickbuttonRecieve(String h) {
		try {
			JSONObject json = new JSONObject();
			json.put("UserName", h);
			json.put("dodawanie", "1");
			HttpParams httpParams = new BasicHttpParams();
			HttpConnectionParams.setConnectionTimeout(httpParams,
					TIMEOUT_MILLISEC);
			HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
			HttpClient client = new DefaultHttpClient(httpParams);
			String url = "http://opiekuj1.javaprovider.net/php/webservice2.php";

			HttpPost request = new HttpPost(url);
			request.setEntity(new ByteArrayEntity(json.toString().getBytes(
					"UTF8")));
			request.setHeader("json", json.toString());
			HttpResponse response = client.execute(request);
			HttpEntity entity = response.getEntity();
			if (entity != null) {
				InputStream instream = entity.getContent();

				String result = RestClient.convertStreamToString(instream);
				Log.i("Read from server", result);
			}
		} catch (Throwable t) {	}
	}


	public class Data {
		// private List<User> users;
		public List<User> users;

		// +getters/setters
	}

	static class User {
		String idusers;
		String UserName;
		String FullName;

		public String getUserName() {
			return UserName;
		}

		public String getidusers() {
			return idusers;
		}

		public String getFullName() {
			return FullName;
		}

		public void setUserName(String value) {
			UserName = value;
		}

		public void setidusers(String value) {
			idusers = value;
		}

		public void setFullName(String value) {
			FullName = value;
		}
	}
}